package com.jtjrenren.android.taxi.driver.manger.api;

import com.jtjrenren.android.taxi.driver.base.BaseApplication;
import com.jtjrenren.android.taxi.driver.bean.DriverInfo;
import com.jtjrenren.android.taxi.driver.bean.Order;
import com.jtjrenren.android.taxi.driver.bean.api.ApiCommonResult;
import com.jtjrenren.android.taxi.driver.bean.api.ApiCompanyList;
import com.jtjrenren.android.taxi.driver.bean.api.ApiDriverInfo;
import com.jtjrenren.android.taxi.driver.bean.api.ApiDriverSr;
import com.jtjrenren.android.taxi.driver.bean.api.ApiOrderCost;
import com.jtjrenren.android.taxi.driver.bean.api.ApiOrderDetails;
import com.jtjrenren.android.taxi.driver.bean.api.ApiOrderList;
import com.jtjrenren.android.taxi.driver.bean.api.ApiPocketInfo;
import com.jtjrenren.android.taxi.driver.bean.api.ApiResult;
import com.jtjrenren.android.taxi.driver.bean.api.ApiResultCommon;
import com.jtjrenren.android.taxi.driver.bean.api.ApiUpVersion;
import com.jtjrenren.android.taxi.driver.bean.api.ApiYYD;
import com.jtjrenren.android.taxi.driver.bean.api.CommonResult;
import com.jtjrenren.android.taxi.driver.manger.AppManger;
import com.jtjrenren.android.taxi.driver.utils.CustomeException;
import com.jtjrenren.android.taxi.driver.utils.GsonUtils;
import com.jtjrenren.android.taxi.driver.R;
import com.jtjrenren.android.taxi.driver.bean.api.ApiBindPayResult;
import com.jtjrenren.android.taxi.driver.bean.api.ApiDriverCarType;
import com.jtjrenren.android.taxi.driver.bean.api.ApiSrResult;
import com.jtjrenren.android.taxi.driver.bean.api.ApiTixian;
import com.jtjrenren.android.taxi.driver.utils.ModuleUtils;
import com.library.utils.AppUtils;
import com.library.utils.LogUtil;
import com.library.utils.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/8/17.
 */

public class ApiWorks {
    private static final String SERVER_URL = AppManger.getInstance().getResourceString(R.string.url_base);
    private static final String UNICODE = AppManger.getInstance().getResourceString(R.string.unicode);
    private static final String COMPANYID = "";

    private static ApiService apiService = creatClient().create(ApiService.class);

    public interface ResponseListener<T> {
        void onResponse(T t);
    }

    //检查版本
    public static void checkAppVersion(final ResponseListener listener) {
        apiService.checkNewVersion(getCommonParams("Public", "CheckVersion"), AppUtils.getVersionName(BaseApplication.getInstance()), "0", "2")
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, ApiUpVersion>() {
                    @Override
                    public ApiUpVersion call(String s) {
                        ApiUpVersion upVersion = null;
                        if (!StringUtils.isEmpty(s))
                            upVersion = GsonUtils.getObj(s, ApiUpVersion.class);
                        return upVersion;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ApiUpVersion>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        //全局异常处理
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(ApiUpVersion apiUpVersion) {
                        if (listener != null)
                            listener.onResponse(apiUpVersion);
                    }
                });
    }

    //获取抢单数量
    public static void getQdCount(String driverID, final ResponseListener<String> listener) {
        apiService.getQdCount(getCommonParams("Order", "getAllOrderQty4Driver"), driverID)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, String>() {
                    @Override
                    public String call(String result) {
                        if (!StringUtils.isEmpty(result)) {
                            ApiCommonResult obj = GsonUtils.getObj(result, ApiCommonResult.class);
                            return obj.getData();
                        }
                        return null;
                    }
                }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(String s) {
                        if (listener != null)
                            listener.onResponse(s);
                    }
                });
    }

    //登录
    public static void login(String phone, String pwdjm, final ResponseListener<DriverInfo> listener) {
        apiService.login(getCommonParams("Driver", "DriverLoginCheck"), phone, pwdjm, COMPANYID)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, DriverInfo>() {
                    @Override
                    public DriverInfo call(String json) {
                        DriverInfo info = null;
                        ApiResult apiResult = GsonUtils.getObj(json, ApiResult.class);
                        if (apiResult != null && apiResult.getResult().indexOf("US") == -1) {
                            String[] split = apiResult.getResult().split(",");
                            if (split.length > 1) {
                                info = new DriverInfo();
                                info.setWorkNo(split[0]);
                                info.setMobilePhone(split[1]);
                            }
                        }
                        return info;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<DriverInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(DriverInfo driverInfo) {
                        if (listener != null)
                            listener.onResponse(driverInfo);
                    }
                });

    }

    //获取司机类型
    public static void getCarType(String driverID, final ResponseListener<DriverInfo> listener) {
        apiService.getDriverCarType(getCommonParams("Driver", "ReturnDriverType"), driverID)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, DriverInfo>() {
                    @Override
                    public DriverInfo call(String jsonStr) {
                        DriverInfo info = null;
                        if (!StringUtils.isEmpty(jsonStr)) {
                            ApiDriverCarType apiDriverCarType = GsonUtils.getObj(jsonStr, ApiDriverCarType.class);
                            if (apiDriverCarType != null && apiDriverCarType.getDataList().size() > 0) {
                                ApiDriverCarType.DataListEntity carTypeEntity = apiDriverCarType.getDataList().get(0);
                                info = new DriverInfo();
                                info.setDriverType(Integer.parseInt(carTypeEntity.getDriverType()));
                                info.setDriverChildType(Integer.parseInt(StringUtils.isEmpty(carTypeEntity.getDriverChildType()) ? "0" : carTypeEntity.getDriverChildType()));
                                info.setTerminalType(carTypeEntity.getTerminalType());
                            }
                        }
                        return info;
                    }
                }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<DriverInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(DriverInfo driverInfo) {
                        if (listener != null)
                            listener.onResponse(driverInfo);
                    }
                });
    }

    //获取司机详情
    public static void getDriverDetails(String driverID, final ResponseListener<DriverInfo> listener) {
        apiService.getDriverDetails(getCommonParams("Driver", "GetDriverDetail"), driverID, COMPANYID)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, DriverInfo>() {
                    @Override
                    public DriverInfo call(String response) {
                        DriverInfo info = null;
                        if (!StringUtils.isEmpty(response))
                            info = GsonUtils.getObj(response, DriverInfo.class);
                        return info;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<DriverInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(DriverInfo driverInfo) {
                        if (listener != null)
                            listener.onResponse(driverInfo);
                    }
                });
    }


    //获取司机的信息详情
    public static void getDriverInfoDetails(String driverID, final ResponseListener<ApiDriverInfo> listener) {
        apiService.getDriverInfoDetails(getCommonParams("Driver", "GetDriverPersonPage"), driverID, COMPANYID)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, ApiDriverInfo>() {
                    @Override
                    public ApiDriverInfo call(String jsonStr) {
                        ApiDriverInfo obj = null;
                        if (!StringUtils.isEmpty(jsonStr))
                            obj = GsonUtils.getObj(jsonStr, ApiDriverInfo.class);
                        return obj;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ApiDriverInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(ApiDriverInfo apiDriverInfo) {
                        if (listener != null)
                            listener.onResponse(apiDriverInfo);
                    }
                });
    }

    //获取今天的收入
    public static void getTodaySr(String driverID, final ResponseListener<ApiDriverSr> listener) {
        apiService.getTodaySr(getCommonParams("Driver", "GetDriverTodayInfo"), driverID)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, ApiDriverSr>() {
                    @Override
                    public ApiDriverSr call(String result) {
                        ApiDriverSr obj = null;
                        if (!StringUtils.isEmpty(result))
                            obj = GsonUtils.getObj(result, ApiDriverSr.class);
                        return obj;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ApiDriverSr>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(ApiDriverSr apiDriverSr) {
                        if (listener != null)
                            listener.onResponse(apiDriverSr);
                    }
                });
    }

    //获取司机的状态
    public static void getDriverStatus(String driverID, Observer observer) {
        Observable<String> observable = apiService.getDriverStatus(getCommonParams("Driver", "GetDriverWorkState"), driverID);
        setSubscribe(observable, observer);
    }

    //获取历史订单
    public static void getOrderHistrory(String driverID, int pageSize,
                                        int currentPage, final ResponseListener<ApiOrderList> listener) {
        apiService.getOrderHistrory(getCommonParams("Driver", "GetHistoryOrderByDriver"), driverID, pageSize, currentPage)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, ApiOrderList>() {
                    @Override
                    public ApiOrderList call(String jsonStr) {
                        ApiOrderList obj = null;
                        if (!StringUtils.isEmpty(jsonStr))
                            obj = GsonUtils.getObj(jsonStr, ApiOrderList.class);
                        return obj;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ApiOrderList>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(ApiOrderList apiOrderList) {
                        if (listener != null)
                            listener.onResponse(apiOrderList);
                    }
                });
    }

    //发送验证码
    public static void sendAuthCode(String phone, String type) {
        Observable<String> observable = apiService.sendAuthCode(getCommonParams("Public", "SendAuthCode"), phone, type);
        setSubscribe(observable, new Observer<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {

            }
        });
    }

    //验证手机号是否唯一
    public static void isUniquePhone(String phone, final ResponseListener<CommonResult> listener) {
        apiService.isUniquePhone(getCommonParams("Public", "IsUniquePhone"), phone, "1", COMPANYID)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, CommonResult>() {
                    @Override
                    public CommonResult call(String jsonStr) {
                        CommonResult result = new CommonResult();
                        result.setCode(0);
                        result.setMsg("");
                        ApiResult obj = GsonUtils.getObj(jsonStr, ApiResult.class);
                        if (obj != null && "US0003".equals(obj.getResult())) //手机号已经被注册 可以进行下一步
                        {
                            result.setCode(1);
                            result.setMsg("手机号已注册");
                        } else
                            result.setMsg("手机号还未注册");
                        return result;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(CommonResult commonResult) {
                        if (listener != null)
                            listener.onResponse(commonResult);
                    }
                });
    }

    //获取公司列表
    public static void getCompanyList(String keyValue, final ResponseListener<ApiCompanyList> listener) {
        apiService.getCompanyList(getCommonParams("Driver", "GetCompanyList"), keyValue)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, ApiCompanyList>() {
                    @Override
                    public ApiCompanyList call(String jsonStr) {
                        ApiCompanyList apiCompanyList = null;
                        if (!StringUtils.isEmpty(jsonStr))
                            apiCompanyList = GsonUtils.getObj(jsonStr, ApiCompanyList.class);
                        return apiCompanyList;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ApiCompanyList>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(ApiCompanyList apiCompanyList) {
                        if (listener != null)
                            listener.onResponse(apiCompanyList);
                    }
                });
    }

    //注册司机
    public static void driverRegister(String phone, String pwd, String userName,
                                      String permitNumber, String carNum, String identityCard,
                                      String driverImageName, String permitNumberImgName, String comPanyID, final ResponseListener<ApiResult> listener) {
        apiService.driverRegister(getCommonParams("Driver", "DriverRegister"), phone, pwd, comPanyID, userName, permitNumber,
                carNum, identityCard, driverImageName, permitNumberImgName)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, ApiResult>() {
                    @Override
                    public ApiResult call(String jsonStr) {
                        ApiResult obj = null;
                        if (!StringUtils.isEmpty(jsonStr))
                            obj = GsonUtils.getObj(jsonStr, ApiResult.class);
                        return obj;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ApiResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(ApiResult apiResult) {
                        if (listener != null)
                            listener.onResponse(apiResult);
                    }
                });
    }


    //检查验证码
    public static void checkYzm(String phoneNO, String keyCode, String type, final ResponseListener<CommonResult> listener) {
        apiService.checkYzm(getCommonParams("Public", "AuthenticatePhoneByCode"), phoneNO, keyCode, type)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, CommonResult>() {
                    @Override
                    public CommonResult call(String jsonStr) {
                        CommonResult result = new CommonResult();
                        result.setCode(0);
                        result.setMsg("验证码校验失败");
                        if (!StringUtils.isEmpty(jsonStr)) {
                            ApiResult obj = GsonUtils.getObj(jsonStr, ApiResult.class);
                            if (obj != null && "SM0009".equals(obj.getResult()))
                                result.setCode(1);
                        }
                        return result;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(CommonResult commonResult) {
                        if (listener != null)
                            listener.onResponse(commonResult);
                    }
                });
    }

    //上传图片
    public static void upImg(String imgBase64Str, final ResponseListener<CommonResult> listener) {
        apiService.upImg(getCommonParams("Public", "UpImage"), imgBase64Str)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, CommonResult>() {
                    @Override
                    public CommonResult call(String jsonStr) {
                        CommonResult result = new CommonResult();
                        result.setCode(0);
                        result.setMsg("图片上传失败");
                        LogUtil.log(ApiWorks.class, "上传的图片result：" + jsonStr);
                        if (jsonStr != null) {
                            ApiResult obj = GsonUtils.getObj(jsonStr, ApiResult.class);
                            if (obj != null && obj.getResult().indexOf("SM") < 0) {
                                result.setData(obj.getResult());
                                result.setCode(1);
                            }
                        }
                        return result;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(CommonResult commonResult) {
                        if (listener != null)
                            listener.onResponse(commonResult);
                    }
                });
    }

    //更新密码
    public static void upPwd(String oldPwd, String newPwd, String driverID, final ResponseListener<CommonResult> listener) {
        apiService.upPwd(getCommonParams("Driver", "ChangePwd"), ModuleUtils.DesEncry(oldPwd), ModuleUtils.DesEncry(newPwd), driverID, COMPANYID)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, CommonResult>() {
                    @Override
                    public CommonResult call(String jsonStr) {
                        CommonResult commonResult = new CommonResult(0, "", "修改失败");
                        if (!StringUtils.isEmpty(jsonStr)) {
                            ApiResult obj = GsonUtils.getObj(jsonStr, ApiResult.class);
                            if (obj != null && "US0020".equals(obj.getResult()))
                                commonResult.setCode(1);
                        }
                        return commonResult;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(CommonResult commonResult) {
                        if (listener != null)
                            listener.onResponse(commonResult);
                    }
                });
    }

    //重置密码
    public static void resetPwd(String pwd, String phoneNo, final ResponseListener<CommonResult> listener) {
        apiService.resetPwd(getCommonParams("Driver", "ResetPassword"), ModuleUtils.DesEncry(pwd),
                 phoneNo, COMPANYID)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, CommonResult>() {
                    @Override
                    public CommonResult call(String jsonStr) {
                        CommonResult commonResult = new CommonResult(0, "", "");
                        if (!StringUtils.isEmpty(jsonStr)) {
                            ApiResult obj = GsonUtils.getObj(jsonStr, ApiResult.class);
                            if (obj != null && "US0010".equals(obj.getResult()))
                                commonResult.setCode(1);
                        }
                        return commonResult;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(CommonResult commonResult) {
                        if (listener != null)
                            listener.onResponse(commonResult);
                    }
                });
    }

    //获取绑定的支付数据
    public static void getPayBindingData(String driverID, int type, final ResponseListener<ApiBindPayResult> listener) {
        apiService.getPayBindingData(getCommonParams("Driver", "GetBankBindingData"), driverID, COMPANYID, type)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, ApiBindPayResult>() {
                    @Override
                    public ApiBindPayResult call(String jsonStr) {
                        if (!StringUtils.isEmpty(jsonStr)) {
                            ApiBindPayResult obj = GsonUtils.getObj(jsonStr, ApiBindPayResult.class);
                            return obj;
                        }
                        return null;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ApiBindPayResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(ApiBindPayResult apiBindPayResult) {
                        if (listener != null)
                            listener.onResponse(apiBindPayResult);
                    }
                });
    }

    //设置绑定数据
    public static void SetBankBindingData_M(String driverID, String accountName, String account, String bankcode, int type,
                                            String pro, String city, final ResponseListener<CommonResult> listener) {
        apiService.SetBankBindingData_M(getCommonParams("Driver", "SetBankBindingData_M"), driverID, accountName, account, bankcode, type, pro, city)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, CommonResult>() {
                    @Override
                    public CommonResult call(String jsonResult) {
                        CommonResult commonResult = new CommonResult(0, "", "");
                        if (!StringUtils.isEmpty(jsonResult)) {
                            ApiResult obj = GsonUtils.getObj(jsonResult, ApiResult.class);
                            if (obj != null && "ok".equalsIgnoreCase(obj.getResult()))
                                commonResult.setCode(1);
                        }
                        return commonResult;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(CommonResult commonResult) {
                        if (listener != null)
                            listener.onResponse(commonResult);
                    }
                });

    }

    //获取钱包信息
    public static void getDriverPocketInfo(String driverID, final ResponseListener<ApiPocketInfo> listener) {
        apiService.getDriverPocketInfo(getCommonParams("Driver", "GetDriverPochetInfo_All"), driverID)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, ApiPocketInfo>() {
                    @Override
                    public ApiPocketInfo call(String jsonStr) {
                        ApiPocketInfo obj = null;
                        if (!StringUtils.isEmpty(jsonStr))
                            obj = GsonUtils.getObj(jsonStr, ApiPocketInfo.class);
                        return obj;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ApiPocketInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(ApiPocketInfo apiPocketInfo) {
                        if (listener != null)
                            listener.onResponse(apiPocketInfo);
                    }
                });
    }

    //提现
    public static void updateRechareMent_M(String driverID, String tradeType, String account, String value, final ResponseListener<ApiCommonResult> listener) {
        apiService.updateRechareMent_M(getCommonParams("Pocket", "UpdateRechareMent_M"), driverID, tradeType, account, ModuleUtils.DesEncry(value))
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, ApiCommonResult>() {
                    @Override
                    public ApiCommonResult call(String jsonStr) {
                        ApiCommonResult obj = null;
                        if (!StringUtils.isEmpty(jsonStr))
                            obj = GsonUtils.getObj(jsonStr, ApiCommonResult.class);
                        return obj;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ApiCommonResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(ApiCommonResult apiCommonResult) {
                        if (listener != null)
                            listener.onResponse(apiCommonResult);
                    }
                });

    }

    //获取提现历史
    public static void getTxHistory(String driverID, final ResponseListener<ApiTixian> listener) {
        apiService.getTxHistory(getCommonParams("Driver", "GetRechargeDetail"), driverID)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, ApiTixian>() {
                    @Override
                    public ApiTixian call(String jsonStr) {
                        ApiTixian obj = null;
                        if (!StringUtils.isEmpty(jsonStr))
                            obj = GsonUtils.getObj(jsonStr, ApiTixian.class);
                        return obj;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ApiTixian>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(ApiTixian apiTixian) {
                        if (listener != null)
                            listener.onResponse(apiTixian);
                    }
                });
    }

    //获取收入明细
    public static void getSrHistory(String driverID, int pageSize, int currentPage, final ResponseListener<ApiSrResult> listener) {
        apiService.getSrHistory(getCommonParams("Driver", "GetIncomeDetail"), driverID, pageSize, currentPage)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, ApiSrResult>() {
                    @Override
                    public ApiSrResult call(String jsonStr) {
                        ApiSrResult obj = null;
                        if (!StringUtils.isEmpty(jsonStr))
                            obj = GsonUtils.getObj(jsonStr, ApiSrResult.class);
                        return obj;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ApiSrResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(ApiSrResult apiSrResult) {
                        if (listener != null)
                            listener.onResponse(apiSrResult);
                    }
                });

    }

    //设置功能接单
    public static void setDriverRarelyDuty(String driverID, String dutyType, String phoneNo, String address, String radius, String changeTime,
                                           String sliceTime, final ResponseListener<CommonResult> listener) {
        apiService.setDriverRarelyDuty(getCommonParams("Driver", "SetDriverRarelyDuty"), driverID, dutyType, phoneNo, address, radius, changeTime, sliceTime)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, CommonResult>() {
                    @Override
                    public CommonResult call(String jsonStr) {
                        CommonResult commonResult = new CommonResult(0, "", "");
                        if (!StringUtils.isEmpty(jsonStr)) {
                            ApiResult obj = GsonUtils.getObj(jsonStr, ApiResult.class);
                            if (obj != null && "ok".equalsIgnoreCase(obj.getResult()))
                                commonResult.setCode(1);
                        }
                        return commonResult;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(CommonResult commonResult) {
                        if (listener != null)
                            listener.onResponse(commonResult);
                    }
                });

    }

    //获取功能设置
    public static void getDutyInfo(String driverID, final ResponseListener<ApiCommonResult> listener) {
        apiService.getDutyInfo(getCommonParams("Driver", "GetRarelyDutyStateAll_M"), driverID)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, ApiCommonResult>() {
                    @Override
                    public ApiCommonResult call(String jsonStr) {
                        ApiCommonResult obj = null;
                        if (!StringUtils.isEmpty(jsonStr)) {
                            jsonStr = jsonStr.replaceAll("\"", "\'").replace("[", "\"[").replace("]", "]\"");
                            obj = GsonUtils.getObj(jsonStr, ApiCommonResult.class);
                        }
                        return obj;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ApiCommonResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(ApiCommonResult apiCommonResult) {
                        if (listener != null)
                            listener.onResponse(apiCommonResult);
                    }
                });
    }

    //设置功能开启--未解析
    public static void SetDriverDutyState(String driverID, String dutyType, String isOn, final ResponseListener<CommonResult> listener) {
        apiService.SetDriverDutyState(getCommonParams("Driver", "SetDriverDutyState"), driverID, dutyType, isOn)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, CommonResult>() {
                    @Override
                    public CommonResult call(String s) {
                        CommonResult commonResult = new CommonResult(1, "", "");
                        return commonResult;
                    }
                }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(CommonResult commonResult) {
                        if (listener != null)
                            listener.onResponse(commonResult);
                    }
                });

    }

    //获取订单详细信息
    public static void getOrderDetails(String orderID, final ResponseListener<Order> listener) {
        apiService.getOrderDetails(getCommonParams("Order", "GetSignalOrderDetail_All_D"), orderID)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, Order>() {
                    @Override
                    public Order call(String s) {
                        Order order = null;
                        if (!StringUtils.isEmpty(s)) {
                            ApiOrderDetails obj = GsonUtils.getObj(s, ApiOrderDetails.class);
                            if (obj != null) {
                                order = new Order();
                                ApiOrderDetails.InfosEntity infos = obj.getInfos();
                                order.setOrderID(infos.getOrderID());
                                order.setOrderType(Integer.parseInt(StringUtils.isEmpty(infos.getOrderType()) ? "2" : infos.getOrderType()));
                                order.setUseDate(infos.getBookingTime());
                                order.setStartAddr(infos.getStartAddr());
                                order.setPassgerLat(infos.getStartLat());
                                order.setPassgerLng(infos.getStartLng());
                                order.setEndAddr(infos.getEndAddr());
                                order.setEndLat(infos.getEndLat());
                                order.setEndLng(infos.getEndLng());
                                order.setTip(infos.getTipsTotal());
                                order.setPassgerPhone(infos.getPassengerPhoneNumber());
                                int status = Integer.parseInt(StringUtils.isEmpty(infos.getStatus()) ? "0" : infos.getStatus());
                                order.setOrderStatus(ModuleUtils.orderStatusAdapter(status));
                                order.setPayStatus(ModuleUtils.orderPayStatusAdapter(infos.getPayStatus()));
                                order.setCreateDate(infos.getCreateDate());
                                order.setTotal(Double.parseDouble(StringUtils.isEmpty(infos.getAmount()) ? "0" : infos.getAmount()));
                                order.setLocalStatus(infos.getStatus());
                            }
                        }
                        return order;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Order>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(Order order) {
                        if (listener != null)
                            listener.onResponse(order);
                    }
                });
    }

    //获取预约单列表
    public static void getYYDList(String driverID, final ResponseListener<List<ApiYYD>> listener) {
        apiService.getYYDList(getCommonParams("Order", "getYYOrderDetail"), driverID)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, List<ApiYYD>>() {
                    @Override
                    public List<ApiYYD> call(String s) {
                        List<ApiYYD> list = new ArrayList<>();
                        if (!StringUtils.isEmpty(s)) {
                            ApiResultCommon apiResultCommon = ApiResultCommon.fromJson(s, ApiYYD.class);
                            if (apiResultCommon != null && apiResultCommon.getData() != null && apiResultCommon.getData().size() > 0)
                                list.addAll(apiResultCommon.getData());
                        }
                        return list;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ApiYYD>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(new ArrayList<ApiYYD>());
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(List<ApiYYD> apiYYDs) {
                        if (listener != null)
                            listener.onResponse(apiYYDs);
                    }
                });
    }

    //获取未完成订单
    public static void getOrderNotFinish(String carNo, String driverID, final ResponseListener<ApiCommonResult> listener) {
        apiService.getOrderNotFinish(getCommonParams("Driver", "getOrderStatus_M"), carNo, driverID)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, ApiCommonResult>() {
                    @Override
                    public ApiCommonResult call(String result) {
                        ApiCommonResult obj = null;
                        if (!StringUtils.isEmpty(result)) {
                            result = result.replaceAll("\"", "\'").replace("[", "\"[").replace("]", "]\"");
                            obj = GsonUtils.getObj(result, ApiCommonResult.class);
                        }
                        return obj;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ApiCommonResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(ApiCommonResult apiCommonResult) {
                        if (listener != null)
                            listener.onResponse(apiCommonResult);
                    }
                });
    }

    //获取月租车后台计费
    public static void getOrderCost(String orderID, final ResponseListener<ApiOrderCost> listener) {
        apiService.getOrderJf(getCommonParams("Z_Cost", "GetCurrentdata"), orderID)
                .map(new Func1<String, ApiOrderCost>() {
                    @Override
                    public ApiOrderCost call(String s) {
                        ApiOrderCost obj = null;
                        if (!StringUtils.isEmpty(s)) {
                            obj = GsonUtils.getObj(s, ApiOrderCost.class);
                        }
                        return obj;
                    }
                })
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ApiOrderCost>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (listener != null)
                            listener.onResponse(null);
                        CustomeException.handException(e);
                    }

                    @Override
                    public void onNext(ApiOrderCost apiOrderCost) {
                        if (listener != null)
                            listener.onResponse(apiOrderCost);
                    }
                });
    }

    private static <T> void setSubscribe(Observable<T> observable, Observer<T> observer) {
        observable.subscribeOn(Schedulers.io())
                .subscribeOn(Schedulers.newThread())//子线程访问网络
                .observeOn(AndroidSchedulers.mainThread())//回调到主线程
                .subscribe(observer);
    }


    public static Map<String, String> getCommonParams(String module, String action) {
        Map<String, String> commonParams = new HashMap<>();
        commonParams.put("__from", "android");
        commonParams.put("__ct", UNICODE);
        commonParams.put("module", module);
        commonParams.put("action", action);

        return commonParams;
    }

    public static Retrofit creatClient() {
        return new Retrofit.Builder()
                .baseUrl(SERVER_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(new OkHttpClient.Builder()
                        .connectTimeout(10, TimeUnit.SECONDS)
                        .writeTimeout(30, TimeUnit.SECONDS)
                        .readTimeout(30, TimeUnit.SECONDS)
                        .build())
                .build();
    }
}
